import { Modal, message } from 'antd';

import React from "react";
import {authBtn} from "../../Components/formTable/FormItems";

const confirm = Modal.confirm;

interface EditDelOperationProps {
    editAuthKey?:string;
    delAuthKey?:string;
    editCallback?:()=>void;
    delCallback?:()=>void;
};

// 表格的 操作栏，有删除、编辑按钮，支持功能权限(在 config/authorize.config.ts 配置)

export function EditDelOperation({editAuthKey, delAuthKey, editCallback=()=>{}, delCallback=()=>{}}:EditDelOperationProps){
  const Edit =authBtn(editAuthKey);
  const Del = authBtn(delAuthKey);
  return (<>
    <Edit key={'edit'} onClick={()=>editCallback()} size={'small'} >编辑</Edit>
    <Del danger key={'delete'} onClick={()=>delCallback()} size={'small'} >删除</Del>
  </>);
}
interface EditDelAuthOperationProps extends EditDelOperationProps{
    authOperationKey?:string;
    authOperationCallback?:()=>void;
}

export const EditDelAuthOperation:React.FC<EditDelAuthOperationProps> =
    ({editCallback=()=>{},delCallback=()=>{},editAuthKey,delAuthKey,authOperationKey,authOperationCallback=()=>{} }) =>{
        const Edit =authBtn(editAuthKey);
        const Del = authBtn(delAuthKey);
        const Auth = authBtn(authOperationKey);
        return (<>
            <Edit key={'edit'} onClick={()=>editCallback()} size={'small'} >编辑</Edit>
            <Del danger key={'delete'} onClick={()=>delCallback()} size={'small'} >删除</Del>
            <Auth key={'auth'} onClick={()=>authOperationCallback()} size={'small'} >授权</Auth>
        </>);
    };

export function delRecord(requestFn:(obj:any)=>Promise<any>,params:string|object,  cName = '', fn = (t:any) =>{} ) {
    confirm({
        // 没办法做到 autoFocusButton， 只能 用cancel的做ok 的工作
        title: `确认删除  ${cName} ?`,
        content: `删除之后,该记录的数据都将 清空，请谨慎操作！！`,
        autoFocusButton: 'cancel',
        cancelText: '确认删除',
        okText: '不删了',
        onOk() {},
        onCancel() {
            requestFn(params).then(res=>{
                message.info("删除成功");
                fn(res);
            }).catch(res=>{
                message.error("删除失败： "+ res);
                console.error(res);
            })
        }
    });
}
/**
 * 根据配置生成 table 的 column 配置；
 * @param title  列名
 * @param key   对应数据库中的字段名
 * @param params    其他配置信息
 * @returns {{title: *, dataIndex: *, key: *}}
 */
export function colItem(title:string, key:string, params = {}) {
    return { title: title, dataIndex: key, key: key, ...params };
}
